/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.st.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jeesite.modules.st.entity.*;
import com.jeesite.modules.st.service.StMaterialRecordService;
import com.jeesite.modules.st.service.StSupplierRecordService;
import com.jeesite.modules.st.service.StTaxRateSettingService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.st.service.StPurchaseBillService;

import java.util.List;

/**
 * 采购单Controller
 *
 * @author HOHD
 * @version 2020-12-16
 */
@Controller
@RequestMapping(value = "${adminPath}/st/stPurchaseBill")
public class StPurchaseBillController extends BaseController {

    @Autowired
    private StPurchaseBillService stPurchaseBillService;

    /**
     * 获取数据
     */
    @ModelAttribute
    public StPurchaseBill get(String id, boolean isNewRecord) {
        return stPurchaseBillService.get(id, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("st:stPurchaseBill:view")
    @RequestMapping(value = {"list", ""})
    public String list(StPurchaseBill stPurchaseBill, Model model) {
        model.addAttribute("stPurchaseBill", stPurchaseBill);
        return "modules/st/stPurchaseBillList";
    }

    @RequestMapping("listSub")
    public String listSub(StPurchaseBill stPurchaseBill, Model model) {
        model.addAttribute("stPurchaseBill", stPurchaseBill);
        List<StPurchaseDetail> sub = stPurchaseBillService.sub(stPurchaseBill);
        return "modules/st/stPurchaseBillList-sub";
    }
    @RequestMapping("detail")
    public String detail(StPurchaseBill stPurchaseBill, Model model) {
        model.addAttribute("stPurchaseBill", stPurchaseBill);
        return "modules/st/stPurchaseBillList-detail";
    }
    @RequestMapping("statistics")
    public String statistics(StPurchaseBill stPurchaseBill, Model model) {
        model.addAttribute("stPurchaseBill", stPurchaseBill);
        return "modules/st/stPurchaseBillList-statistics";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("st:stPurchaseBill:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<StPurchaseBill> listData(StPurchaseBill stPurchaseBill, HttpServletRequest request, HttpServletResponse response) {
        stPurchaseBill.setPage(new Page<>(request, response));
        Page<StPurchaseBill> page = stPurchaseBillService.findPage(stPurchaseBill);
        return page;
    }

    @RequiresPermissions("st:stPurchaseBill:view")
    @RequestMapping(value = "listDataSub")
    @ResponseBody
    public Page<StPurchaseDetail> listDataSub(StPurchaseBill stPurchaseBill, HttpServletRequest request, HttpServletResponse response) {
        Page<StPurchaseDetail> page = new Page<>(request, response);
        stPurchaseBill.setPage(page);
        List<StPurchaseDetail> sub = stPurchaseBillService.sub(stPurchaseBill);
        page.setList(sub);
        return page;
    }
    @RequiresPermissions("st:stPurchaseBill:view")
    @RequestMapping(value = "listDetail")
    @ResponseBody
    public Page<StPurchaseDetail> listDetail(StPurchaseDetail stPurchaseDetail, HttpServletRequest request, HttpServletResponse response) {
        Page<StPurchaseDetail> page = new Page<>(request, response);
        stPurchaseDetail.setPage(page);
        List<StPurchaseDetail> sub = stPurchaseBillService.findDetailWarehouse(stPurchaseDetail);
        page.setList(sub);
        return page;
    }
    @RequiresPermissions("st:stPurchaseBill:view")
    @RequestMapping(value = "listStatistics")
    @ResponseBody
    public Page<StPurchaseDetail> listStatistics(StPurchaseDetail stPurchaseDetail, HttpServletRequest request, HttpServletResponse response) {
        Page<StPurchaseDetail> page = new Page<>(request, response);
        stPurchaseDetail.setPage(page);
        List<StPurchaseDetail> sub = stPurchaseBillService.findStatistics(stPurchaseDetail);
        page.setList(sub);
        return page;
    }

    @Autowired
    StSupplierRecordService stSupplierRecordService;
    @Autowired
    StMaterialRecordService stMaterialRecordService;
    ObjectMapper objectMapper = new ObjectMapper();

    @Autowired
    StTaxRateSettingService stTaxRateSettingService;

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("st:stPurchaseBill:view")
    @RequestMapping(value = "form")
    public String form(StPurchaseBill stPurchaseBill, Model model) throws JsonProcessingException {
        List<StMaterialRecord> materialList = stMaterialRecordService.findList(new StMaterialRecord());
        List<StTaxRateSetting> rateList = stTaxRateSettingService.findList(new StTaxRateSetting());
        List<StSupplierRecord> supplierList = stSupplierRecordService.findList(new StSupplierRecord());
        model.addAttribute("supplierList", supplierList);
        model.addAttribute("materialList", objectMapper.writeValueAsString(materialList));
        model.addAttribute("rateList", objectMapper.writeValueAsString(rateList));
        model.addAttribute("stPurchaseBill", stPurchaseBill);
        return "modules/st/stPurchaseBillForm";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("st:stPurchaseBill:edit")
    @PostMapping(value = "save")
    @ResponseBody
    public String save(@Validated StPurchaseBill stPurchaseBill) {
        stPurchaseBillService.save(stPurchaseBill);
        stPurchaseBill.setStPurchaseDetailList(null);
        return renderResult(Global.TRUE, text("保存采购单成功！"), stPurchaseBill);
    }

    /**
     * 删除数据
     */
    @RequiresPermissions("st:stPurchaseBill:edit")
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(StPurchaseBill stPurchaseBill) {
        stPurchaseBillService.delete(stPurchaseBill);
        return renderResult(Global.TRUE, text("删除采购单成功！"));
    }

}